﻿using System;
using System.Collections.Generic;

using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using CNVP.JShop.UI;
using PayWayAPI.TenPayWay;

namespace CNVP.JShop.Web.Member
{
    public partial class Tenpay_Return : UserPage
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            //密钥
            string key = "";
            var model_Col = new BLL.CNVP_Column().GetSingle_CNVP_ColumnById(31);//  ID=31 是财富通
            if (model_Col != null)
            {
                //密钥
                key = model_Col.PayKey;
            }

            //创建PayResponseHandler实例
            MediPayResponse resHandler = new MediPayResponse(Context);

            resHandler.setKey(key);

            //判断签名
            if (resHandler.isTenpaySign())
            {

                //支付结果
                string retcode = resHandler.getParameter("retcode");

                //支付状态
                string status = resHandler.getParameter("status");

                //商户订单号
                string mch_vno = resHandler.getParameter("mch_vno");

                //商品价格
                string mch_price = resHandler.getParameter("total_fee");

                if ("0".Equals(retcode))
                {
                    //Response.Write("status:" + status + "<br/>");

                    //------------------------------
                    //处理业务开始
                    //------------------------------ 

                    //按状态处理相关业务逻辑
                    switch (Int32.Parse(status))
                    {
                        case 1:
                            //交易创建
                            break;
                        case 2:
                            //收获地址填写完毕
                            break;
                        case 3:
                            //买家付款成功，注意判断订单是否重复的逻辑
                            break;
                        case 4:
                            //卖家发货成功
                            break;
                        case 5:
                            //买家收货确认，交易成功
                            break;
                        case 6:
                            //交易关闭，未完成超时关闭
                            break;
                        case 7:
                            //修改交易价格成功
                            break;
                        case 8:
                            //买家发起退款
                            break;
                        case 9:
                            //退款成功
                            break;
                        case 10:
                            //退款关闭
                            break;
                        default:
                            //error	
                            break;
                    }
                    //调用doShow, 打印meta值跟js代码,告诉财付通处理成功,并在用户浏览器显示$show页面.
                    resHandler.doShow();

                }
                else
                {
                    string html = "";
                    html = "------------------------------------------";
                    html += "请在财富通中查看是否支付成功。<br>成功后与客服联系！";
                    html += "------------------------------------------";
                    html += "<br>Result:responseTxt=" + retcode;
                    html += "支付失败";                                             //支付失败，提示信息
                    LabMSG.Text = html;
                    //当做不成功处理
                }

            }
            else
            {
                string html = "";
                html = "------------------------------------------";
                html += "请在财富通中查看是否支付成功。<br>成功后与客服联系！";
                html += "------------------------------------------";
                html += "<br>Result:sign=" + resHandler.isTenpaySign();
                html += "认证签名失败";                                             //支付失败，提示信息
                LabMSG.Text = html;
            }
        }
    }
}